package cc.fine.svc.main.service.impl.tbl;

import cc.fine.svc.common.utils.R;
import cc.fine.svc.main.bean.dto.CommonInfoDto;
import cc.fine.svc.main.bean.po.TblCommon;
import cc.fine.svc.main.bean.po.TblUser;
import cc.fine.svc.main.bean.vo.CommonPublishVo;
import cc.fine.svc.main.bean.vo.CommonViewVo;
import cc.fine.svc.main.mapper.TblCommonMapper;
import cc.fine.svc.main.service.VideoCountService;
import cc.fine.svc.main.service.tbl.TblCommonService;
import cc.fine.svc.main.service.tbl.TblUserService;
import cc.fine.svc.main.utils.EsUtil;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wyihe
 * @since 2023-10-25
 */
@Service
@Transactional
public class TblCommonServiceImpl extends ServiceImpl<TblCommonMapper, TblCommon> implements TblCommonService {


    @Autowired
    private VideoCountService countService;

    @Autowired
    private TblUserService userService;

    @Autowired
    private EsUtil esUtil;

    @Override
    public void publish(CommonPublishVo vo) {
        TblCommon common = new TblCommon();
        String commonId = UUID.randomUUID().toString();
        common.setId(commonId);

        BeanUtils.copyProperties(vo, common);

        save(common);
        countService.increaseCommon(vo.getVideoId());
    }

    @Override
    public List<CommonViewVo> commons(String videoId, Integer page, Integer limit) {
        // 查找父级
        List<TblCommon> rootList = getRootCommonByVideoId(videoId, page, limit);

        // 查找父级的所有子级
        List<String> fIds = rootList.stream()
                .map(x -> x.getId())
                .collect(Collectors.toList());

        // 查找子级别
        List<TblCommon> childList = getChildCommonByFaIds(fIds);

        List<String> userIds = rootList.stream()
                .map(x -> x.getUserId())
                .collect(Collectors.toList());

        List<String> userIdsChild = childList.stream()
                .map(x -> x.getUserId())
                .collect(Collectors.toList());

        // 合并去重
        userIds.removeAll(userIdsChild);
        userIds.addAll(userIdsChild);


        List<TblUser> userList = userService.getByIdBatch(userIds);

        Map<String, TblUser> userMap = userList.stream()
                .collect(Collectors.toMap(x -> x.getId(), x -> x));

        List<CommonInfoDto> rootRes = new ArrayList<>();
        for (TblCommon common : rootList) {
            CommonInfoDto dto = new CommonInfoDto();
            dto.setCommon(common);
            dto.setUser(userMap.get(common.getUserId()));

            rootRes.add(dto);
        }
        List<CommonInfoDto> childRes = new ArrayList<>();
        for (TblCommon common : childList) {
            CommonInfoDto dto = new CommonInfoDto();
            dto.setCommon(common);
            dto.setUser(userMap.get(common.getUserId()));

            childRes.add(dto);
        }

        Map<String, List<CommonInfoDto>> map = new HashMap<>();
        for (CommonInfoDto e : childRes) {
            String pid = e.getCommon().getPid();
            List<CommonInfoDto> value = map.get(pid);
            if (value == null) value = new ArrayList<>();
            value.add(e);

            map.put(pid, value);
        }

        List<CommonViewVo> res = new ArrayList<>();
        for (CommonInfoDto e : rootRes) {
            CommonViewVo vo = new CommonViewVo();
            vo.setRoot(e);
            vo.setChild(map.get(e.getCommon().getId()));
            res.add(vo);
        }


        return res;
    }

    private List<TblCommon> getChildCommonByFaIds(List<String> fIds) {
        if (fIds.isEmpty()) return new ArrayList<>();
        List<TblCommon> res = baseMapper.getChildCommonByFaIds(fIds);
        if (res == null) res = new ArrayList<>();
        return res;
    }

    private List<TblCommon> getRootCommonByVideoId(String videoId, Integer page, Integer limit) {

        List<TblCommon> res = baseMapper.getRootCommonByVideoId(videoId, page * limit, limit);
        if (res == null) res = new ArrayList<>();
        return res;
    }
}
